package com.study.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.pojo.Headline;
import com.study.pojo.vo.PortalVo;
import com.study.service.HeadlineService;
import com.study.mapper.HeadlineMapper;
import com.study.service.TypeService;
import com.study.utils.JwtHelper;
import com.study.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author 路明非
* @description 针对表【news_headline】的数据库操作Service实现
* @createDate 2025-02-28 18:21:46
*/
@Service
public class HeadlineServiceImpl extends ServiceImpl<HeadlineMapper, Headline>
    implements HeadlineService{
    @Autowired
    private HeadlineMapper headlineMapper;//mapper
    @Autowired
    private JwtHelper jwtHelper;//jwt工具类

    /**
     * 分页数据查询
     * 1.进行分页查询
     * 2.将查询到的数据封装到分页对象中
     * 查询需要自定义sql语句 自定义mapper的方法
     * 返回的结果List<Map>
     * @param portalVo
     * @return
     */
    @Override
    public Result findNewsPage(PortalVo portalVo) {

        IPage<Map> page=new Page<>(portalVo.getPageNum(),portalVo.getPageSize());
        headlineMapper.selectMyPage(page,portalVo);
        List<Map> records = page.getRecords();
        //封装结果
       Map data = new HashMap<>();
       data.put("pageData",records);
       data.put("pageNum",page.getCurrent());
       data.put("pageSize",page.getSize());
       data.put("totalPage",page.getPages());
       data.put("totalSize",page.getTotal());

       Map pageInfo = new HashMap<>();
       pageInfo.put("pageInfo",data);
       return Result.ok(pageInfo);
    }

    /**
     * 根据id查询头条详情
     * 1.查询后修改阅读量+1（需要先查询乐观锁设置的版本号）
     * @param hid
     * @return
     */
    @Override
    public Result showHeadlineDetail(Integer hid) {
        Map data=headlineMapper.queryDatilMap(hid);
        Map headlineMap=new HashMap();
        headlineMap.put("headline",data);
        //修改阅读量
        Headline headline = new Headline();
        headline.setHid((Integer) data.get("hid"));//设置id成为主键
        headline.setVersion((Integer) data.get("version"));//设置乐观锁的版本号
        headline.setPageViews((Integer) data.get("pageViews")+1);
        headlineMapper.updateById(headline); //根据主键修改版本号
        return Result.ok(headlineMap);


    }

    /**
     * 发布头条
     * @param headline
     * @return
     */

    @Override
    public Result publish(Headline headline,String token) {
        //token查询用户id
        int userId = jwtHelper.getUserId(token).intValue();
        //装入数据
        headline.setPublisher(userId);
        headline.setPageViews(0);
        headline.setCreateTime(new Date());
        headline.setUpdateTime(new Date());
        //保存数据
        headlineMapper.insert(headline);

        return Result.ok(null);
    }

    /**
     * desc 修改头条数据
     * 1.根据id查询头条数据最新版本号version
     * 2.更新修改时间
     * @param headline
     * @return
     */
    @Override
    public Result updateData(Headline headline) {
        Integer version = headlineMapper.selectById(headline.getHid()).getVersion();
        headline.setVersion(version); //乐观锁
        headline.setUpdateTime(new Date());
        headlineMapper.updateById(headline);
        return Result.ok(null);

    }
    /**
     * 删除头条
     * @param hid
     * @return
     */
}




